package com.sakiko.controller;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
@RequestMapping("share")
public class ShareController {

    @Autowired
    private ServletContext servletContext;

    /**
     * servletContext [项目内共享
     * 1、以小博大，通过小的共享域可以获取大的共享域
     * 2、servletContext 会加入到 ioc容器 【全局唯一
     * httpSession [一次会话
     * request [一次请求
     */
    // 需求 1: get /share/show 在三大共享域 存储 key = value
    @GetMapping("show")
    public String show(HttpServletRequest request, HttpSession session) {
        request.setAttribute("key", "value");
        session.setAttribute("key", "value");
//        ServletContext servletContext = session.getServletContext(); // request.getServletContext()
//        servletContext.setAttribute("key", "value");
        servletContext.setAttribute("key", "value");
        return "show";
    }
}
